<!--
 * @Author: Cenven
 * @Date: 2021-08-02 10:52:39
 * @Description: 北京泓深教育发展有限公司
 * @Descripttion: 
 * @version: @Lastest
 * @LastEditTime: 2021-08-10 14:56:17
 * @CopyRight: 北京泓深教育发展有限公司
-->
<template>
  <div class="page-mobile-intention">
    <white-header
      :isShowTopNav="true"
      :isShowBack="false"
      :isShowRight="true"
      title="完善信息"
    ></white-header>
    <div class="intention_container">
      <van-steps :active="active" style="margin-bottom:.2rem">
        <van-step>当前年级</van-step>
        <van-step>报考院校</van-step>
        <van-step>提交完成</van-step>
      </van-steps>
      <van-form @submit="onSubmit">
        <van-field
          v-show="active == 0"
          readonly
          clickable
          name="picker"
          :value="formatValue(gradeList, form.gradeId)"
          label="当前年级"
          placeholder="点击选择年级"
          @click="showGradePicker = true"
        />
        <van-field
          v-show="active == 1"
          readonly
          clickable
          name="picker"
          :value="formatValue(schoolList, form.schoolCollegeId)"
          label="报考院校"
          placeholder="点击选择报考院校"
          @click="showSchoolPicker = true"
        />
        <div v-show="active == 2 && !success" style="text-align:center;">
          <van-icon name="flower-o" color="var(--theme-color)" size="1.4rem" />
          <p class="tips_info">信息填写完成啦，请点击确定按钮进行提交哟！</p>
        </div>
        <div v-show="active == 2 && success" style="text-align:center;">
          <van-icon name="success" color="#07c160" size="1.4rem" />
          <p class="tips_info">提交成功，请点击下面按钮进入首页哟！</p>
        </div>
        <div class="intention_btn_list" v-show="!success">
          <div
            class="intention_btn"
            v-show="active != 0"
            @click="stepHandle('prev')"
          >
            上一步
          </div>
          <div
            class="intention_btn"
            v-show="active != 2"
            @click="stepHandle('next')"
          >
            下一步
          </div>
          <button
            class="intention_btn"
            v-show="active == 2"
            native-type="submit"
          >
            提交
          </button>
        </div>
        <div class="intention_btn_list" v-show="success">
          <div class="intention_btn" @click="$router.replace('/m')">
            进入首页
          </div>
        </div>
      </van-form>
    </div>
    <van-popup v-model="showGradePicker" position="bottom">
      <van-picker
        title="年级"
        show-toolbar
        :columns="gradeList"
        value-key="name"
        @confirm="onGradeConfirm"
        @cancel="showGradePicker = false"
      />
    </van-popup>
    <van-popup v-model="showSchoolPicker" position="bottom">
      <van-picker
        title="报考院校"
        show-toolbar
        :columns="schoolList"
        value-key="name"
        @confirm="onSchoolConfirm"
        @cancel="showSchoolPicker = false"
      />
    </van-popup>
  </div>
</template>

<script>
import WhiteHeader from "~/components/mobile/WhiteHeader.vue";
import { getGradeSchoolList, updateGradeSchoolInfo } from "~/api/account/user.js";
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  scrollToTop: true,
  components: {
    WhiteHeader
  },
  data() {
    return {
      that: this,
      success: false,
      active: 0,
      flag:false,
      gradeList: [],
      schoolList: [],
      showGradePicker: false,
      showSchoolPicker: false,
      form: {
        gradeId: null,
        schoolCollegeId: null
      }
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      token(state) {
        return state.token;
      },
      is_loading(state) {
        return state.is_loading;
      }
    }),
    title() {
      switch (this.active) {
        case 0:
          return "当前年级";
        case 1:
          return "报考院校";
        case 2:
          return "提交完成";
      }
    }
  },
  watch: {},
  filters: {},
  activated() {
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }else if(this.user_info.gradeId && this.user_info.schoolCollegeId){
        this.$router.back();
      }
    }
    this.initPage();
  },
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    ...mapActions(["initUser"]),
    stepHandle(type) {
      if (type == "prev") {
        this.active--;
      } else if (type == "next") {
        if (this.active == 0) {
          if (this.form.gradeId) {
            this.active++;
          } else {
            this.$toast("请选择当前年级信息再进行下一步");
          }
        } else if (this.active == 1) {
          if (this.form.schoolCollegeId) {
            this.active++;
          } else {
            this.$toast("请选择报考院校信息再进行下一步");
          }
        }
      }
    },
    formatValue(option, type) {
      if (!type) {
        return "";
      }
      let obj = option.find(item => item.id == type);
      if (obj) {
        return obj.name;
      } else {
        return "";
      }
    },
    onGradeConfirm(value, index) {
      this.form = {
        ...this.form,
        gradeId: value.id
      };
      this.showGradePicker = false;
    },
    onSchoolConfirm(value, index) {
      this.form = {
        ...this.form,
        schoolCollegeId: value.id
      };
      this.showSchoolPicker = false;
    },
    async onSubmit() {
        if(this.flag){
            return;
        }
        this.flag = true;
        this.SET_IS_LOADING(true);
        let params = {
            id:this.user_info.id,
            ...this.form
        }
        let result = await updateGradeSchoolInfo(params);
        if (result && result.data && result.data.code == 200) {
            this.success = true;
            this.flag = false;
            this.initUser();
        } else {
            this.$toast("提交失败");
        }
        this.SET_IS_LOADING(false);
    },
    async getData() {
      this.SET_IS_LOADING(true);
      let result = await getGradeSchoolList();
      if (result && result.data && result.data.code == 200) {
        this.gradeList = result.data.data.grades;
        this.schoolList = result.data.data.schoolColleges;
      } else {
        this.$toast("获取失败");
      }
      this.SET_IS_LOADING(false);
    },
    formatDate(date) {
      var date = new Date(date);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD;
      //   return YY + MM + DD + " " + hh + mm + ss;
    },
    initPage() {
      this.form = {
        gradeId: null,
        schoolCollegeId: null
      };
      this.success = false;
      this.active = 0;
      this.flag = false;
      this.getData();
    }
  },
  created() {},
  mounted() {}
};
</script>

<style lang="scss" scoped>
.page-mobile-intention {
  width: 100vw;
  height: 100vh;
  background: #f4f4f4;
  //   padding: 0.288rem;
  // background: #fff;
  box-sizing: border-box;
  .intention_container {
    .tips_info {
      font-size: 0.2688rem;
      text-align: center;
      padding: 0.2rem 0;
    }
    .intention_btn_list {
      display: flex;
      justify-content: space-around;
      margin-top: 0.5rem;
      .intention_btn {
        width: 3rem;
        height: 0.7rem;
        text-align: center;
        color: #fff;
        background: var(--theme-color);
        border: none;
        font-size: 0.3rem;
        line-height: 0.7rem;
        border-radius: 0.05rem;
      }
    }
    .submit_btn {
      margin-top: 0.5rem;
      width: 100%;
      height: 0.7rem;
      text-align: center;
      color: #fff;
      background: var(--theme-color);
      border: none;
      font-size: 0.3rem;
      line-height: 0.7rem;
      border-radius: 0.05rem;
    }
  }
}
</style>
<style lang="scss">
.page-mobile-intention {
}
</style>
